﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LightCAD.Drawing.Actions
{
    public class ComponentInstance2dAction : ElementAction
    {
        public ComponentInstance2dAction():base() { }
        public ComponentInstance2dAction(IDocumentEditor docEditor) : base(docEditor)
        {

        }
        public override void Draw(SKCanvas canvas, LcElement element, Matrix3 matrix)
        {
            
            var cptIns = element as LcComponentInstance;
            var curves = cptIns?.Curves?.SelectMany(cs => cs.Curve2ds);
            if (curves == null)
            {
                return;
            }
            foreach (var curve in curves)
            {
                var points = curve.GetPoints(-1, this.vportRt.Viewport.Scale);
                for (int i = 0; i < points.Length; i++)
                {
                    var point = points[i];
                    point.ApplyMatrix3(matrix);
                }
                canvas.DrawPoints(SKPointMode.Polygon, points.Select(p => this.vportRt.ConvertWcsToScr(p).ToSKPoint()).ToArray(), Constants.defaultPen);
            }
        }
        public override void Draw(SKCanvas canvas, LcElement element, Vector2 offset)
        {
            var cptIns = element as IComponentInstance;
            var curves = cptIns.Curves.SelectMany(cs => cs.Curve2ds);
            foreach (var curve in curves)
            {
                var points = curve.GetPoints(-1, this.vportRt.Viewport.Scale);
                for (int i = 0; i < points.Length; i++)
                {
                    var point = points[i];
                    point.Add(offset);
                }
                canvas.DrawPoints(SKPointMode.Polygon, points.Select(p => this.vportRt.ConvertWcsToScr(p).ToSKPoint()).ToArray(), Constants.defaultPen);
            }
        }
    }
}
